/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author asmaa
 */
public class Sequencer extends javax.swing.JInternalFrame {

    //Class Objects
    DrawCurve obj;

    /**
     * Creates new form Sequencer
     */
    public Sequencer(String sequence, int length, String inputDialog) {
        //Change default appearance location of the frame
        setLocation(260, 140);
        //Declaring JPanel(DrawCurve) that will show sequence
        obj = new DrawCurve(sequence.toUpperCase(), length, inputDialog);

        initComponents();

        //Adding DrawCurve panel to the scroll pane 
        seqJScrollPane.getViewport().add(obj);
    }

    /*
     * This function used to return reference to JPanel containing curve
     */
    public DrawCurve getDrawCurve() {
        return obj;
    }
    /*
     * This flag used to determine if this sequence is saved or not
     */
    boolean saved = false;

    /*
     * This function used to set save flag value
     */
    public void setSaved(boolean state) {
        saved = state;
    }

    /*
     * This function used to determine save status of this sequence
     */
    public boolean isSaved() {
        return saved;
    }

    /*
     * This function used to check save status if not saved then save it else
     * close successfully
     */
    public void closeWindow() {
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        if (!this.isSaved()) {
            int result = JOptionPane.showConfirmDialog(this, "Do you want to save this file?", "Save", JOptionPane.YES_NO_OPTION);
            switch (result) {
                case JOptionPane.YES_OPTION:
                    frm.getSaveAsButton().doClick();
                    break;
            }
            frm.getToolbox().setToolsEnabled(false);
            frm.setMenuShapeToolsEnabled(false);
            frm.getPositioningToolbar().setVisible(false);
            frm.getCompanyDataBtn().setEnabled(false);
            frm.getLogoBtn().setEnabled(false);
            frm.setEditingButtonsEnable(false);
            frm.getDualButton().setEnabled(false);
        }
        this.dispose();
    }

    /*
     * Overrided Close Operation
     */
    @Override
    public void doDefaultCloseAction() {
        closeWindow();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        seqJScrollPane = new javax.swing.JScrollPane();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setPreferredSize(new java.awt.Dimension(800, 600));
        setVisible(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameDeiconified(evt);
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameIconified(evt);
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(seqJScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 798, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(seqJScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setEditingButtonsEnable(true);
        frm.setMenuShapeToolsEnabled(false);
        frm.getToolbox().setToolsEnabled(false);
        frm.getPositioningToolbar().setVisible(false);
        frm.setSaveEnabled(false);
        frm.getCompanyDataBtn().setEnabled(true);
        frm.getLogoBtn().setEnabled(true);
        frm.getDualButton().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameActivated

    private void formInternalFrameIconified(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameIconified
        // TODO add your handling code here:
        boolean flag = false;
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.getCompanyDataBtn().setEnabled(false);
        frm.getLogoBtn().setEnabled(false);
        for (JInternalFrame jf : frm.getDesktopPane().getAllFrames()) {
            if (!jf.isIcon() && (jf.getClass().getName().equals("PCR.WorkArea")
                    || jf.getClass().getName().equals("PCR.Sequencer"))) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            frm.setEditingButtonsEnable(false);
        }
        frm.getDualButton().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameIconified

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setEditingButtonsEnable(true);
        frm.setSaveEnabled(false);
        frm.getCompanyDataBtn().setEnabled(true);
        frm.getLogoBtn().setEnabled(true);
        frm.getDualButton().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameOpened

    private void formInternalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameDeiconified
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setEditingButtonsEnable(true);
        frm.setSaveEnabled(false);
        frm.getCompanyDataBtn().setEnabled(true);
        frm.getLogoBtn().setEnabled(true);
        frm.getDualButton().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameDeiconified
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane seqJScrollPane;
    // End of variables declaration//GEN-END:variables
}
